<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Protype extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->TypeModel=model('Product.Type');
    }

    public function index(){
        $keywords=$this->request->param('keywords','');

        // 封装一个查询条件的数值
        $where=[];

        if(!empty($keywords)){
            $where['name']=['like',"%$keywords%"];
        }

        $typelist=$this->TypeModel->where($where)->order('weigh','asc')->paginate(10);

        $count=$this->TypeModel->count();

        $pagestr=$typelist->render();
        
        $this->assign([
            'typelist'=>$typelist,
            'count'=>$count,
            'pagestr'=>$pagestr,
            'keywords'=>$keywords,
        ]);

        return $this->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $name=$this->request->param('name','');
            $weigh=$this->request->param('weigh',0);

            $type=$this->TypeModel->where(['name'=>$name])->find();

            if($type){
                $this->error('该分类已存在，请重新输入');
                exit;
            }

            if(empty($weigh)){
                $this->error('请为该分类定义一个权重');
                exit;
            }

            $data=[
                'name'=>$name,
                'weigh'=>$weigh
            ];

            if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0){
                $result=upload('cover');

                if($result===FALSE){
                    $this->error('添加图标失败');
                    exit;
                }

                // 追加图标
                $data['cover']='uploads/'.$result;
            }

            $TypeStatus=$this->TypeModel->save($data);

            if($TypeStatus===FALSE){
                $this->error($this->TypeModel->getError());
                exit;
            }else{
                $this->success('添加分类成功','/admin/protype/index');
                exit;
            }
        }

        return $this->fetch();
    }

    public function edit(){

        $typeid=$this->request->param('typeid',0);

        $type=$this->TypeModel->find($typeid);

        if($this->request->isPost()){
            $name=$this->request->param('name','');
            $weigh=$this->request->param('weigh',0);

            $typedata=$this->TypeModel->where(['name'=>$name])->where('id','<>',$typeid)->find();

            if($typedata){
                $this->error('该分类已存在，请重新输入');
                exit;
            }

            $data=[
                'id'=>$typeid,
                'name'=>$name,
                'weigh'=>$weigh
            ];

            if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0){
                $result=upload('cover');

                if($result===FALSE){
                    @is_file($result) && @unlink($result);
                    $this->error('修改图标失败');
                    exit;
                }

                // 追加图标
                $data['cover']='uploads/'.$result;
            }

            $TypeStatus=$this->TypeModel->isUpdate(true)->save($data);

            if($TypeStatus===FALSE){
                if(isset($data['cover'])){
                    @is_file($data['cover']) && @unlink($data['cover']);
                }
                
                $this->error($this->TypeModel->getError());
                exit;
            }else{
                if(isset($data['cover'])){
                    $old=$type['cover'];
                    @is_file($old) && @unlink($old);
                }
                
                $this->success('修改成功','admin/protype/index');
                exit;
            }
        }

        $this->assign([
            'type'=>$type,
        ]);

        return $this->fetch();
    }

    // 删除
    public function del()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids',0);
            
            $result = $this->TypeModel->destroy($ids);

            if($result === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->TypeModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除成功';
            }
            return json($success);
        }
    }
}